package opera;

import book.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 邓大帅
 * Date: 2022-12-20
 * Time: 10:10
 */
public class DelOperation implements IOperation {
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("输入你要删除图书的名字");
        Scanner input = new Scanner(System.in);
        String name = input.nextLine();
        int currentSize = bookList.getUseSize();
        int index = -1;
        for (int i = 0; i < currentSize; i++) {
            Book temp = bookList.getBook(i);
            if (temp.getName().equals(name)) {
               index = i;
               break;
            }
        }
        for (int i = index; i < currentSize-1; i++) {
            Book book = bookList.getBook(i+1);
            bookList.setBook(i,book);
        }
        bookList.setUseSize(currentSize-1);
        bookList.setBook(currentSize-1,null);
        System.out.println("删除成功");
    }
}
